Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

144
Views
convertir este código a una versión sin "async - await"

Estoy tratando de convertir este código a una versión sin "async - await". Código convertido para usar (todas) las promesas sin async y await. Pero mi consola se imprime indefinida por tiempo. ¿Alguien me puede ayudar? Es para una tarea de mi facultad. Muchas gracias

 (async() => { function exibirErro(err) { console.log(err.message); } const f1 = async(tempo) => { return new Promise((resolve, reject) => { const f = () => reject(new Error('Rejeitado')); console.log(`Aguardando ${tempo} segundos...`); setTimeout(f, tempo); }); }; const f2 = async(x) => { if ((Math.random() * 10) % 2 == 0) { throw new Error('Ops'); } return Promise.resolve(1000 * x); }; const f3 = async() => { const r = await new Promise(resolve => resolve(2)); const t = await f2(r); await f1(t); }; try { await f3(); } catch (err) { exibirErro(err); } })();

Mi código:

 function exibirErro(err) { console.log(err.message); } function f1(tempo) { return new Promise((resolve, reject) => { const f = () => reject(new Error('Reject')); console.log(`wait ${tempo} seconds...`); setTimeout(f, tempo); }) .then((x) => { if ((Math.random() * 10) % 2 == 0) { throw new Error('Ops'); } return Promise.resolve(1000 * x); }) .then((t) => { return Promisse.resolve(f1(t)); }).catch((e) => { exibirErro(e); }) } f1();
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Comenzando con su función de envoltura -

 (async() => {

Primero tenemos una función síncrona normal, nada que cambiar aquí -

 function exibirErro(err) { console.log(err.message); }

Luego tenemos la función f1 que usa la palabra clave async pero no await nada. Todo lo que tiene que hacer aquí es eliminar la palabra clave async :

 const f1 = async(tempo) => { return new Promise((resolve, reject) => { const f = () => reject(new Error('Rejeitado')); console.log(`Aguardando ${tempo} segundos...`); setTimeout(f, tempo); }); };

Luego tenemos la función f2 , que nuevamente usa async pero no await nada. Simplemente elimine async -

 const f2 = async(x) => { if ((Math.random() * 10) % 2 == 0) { throw new Error('Ops'); } return Promise.resolve(1000 * x); };

Entonces tenemos la función f3 que await s dos cosas, r y t -

 const f3 = async() => { const r = await new Promise(resolve => resolve(2)); const t = await f2(r); await f1(t); // missing return };

Podemos reescribir f3 usando .then -

 const f3 = () => new Promise(resolve => resolve(2)).then(r => f2(r).then(t => f1(t) ) )

Finalmente tienes try / catch -

 try { await f3(); } catch (err) { exibirErro(err); }

Simplemente use .catch en su lugar -

 f3().catch(exibirErro)

Y la sintaxis de cierre para la función contenedora async . El contenedor no será necesario en la versión final.

 })();

Aquí está todo junto ahora -

 function exibirErro(err) { console.log(err.message); } const f1 = tempo => { return new Promise((resolve, reject) => { const f = () => reject(new Error('Rejeitado')); console.log(`Aguardando ${tempo} segundos...`); setTimeout(f, tempo); }); }; const f2 = x => { if ((Math.random() * 10) % 2 == 0) { throw new Error('Ops'); } return Promise.resolve(1000 * x); }; const f3 = () => { return new Promise(resolve => resolve(2)).then(r => f2(r).then(t => f1(t) ) ) } f3().catch(exibirErro)
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!